<?php
/**
 * Created by PhpStorm.
 * User: 张秀雨
 * Date: 2019/1/14
 * Time: 15:26
 */
namespace app\common\model;


class Lagou extends BankModel
{
    //爬取url地址
    protected $url;
    protected $cookie_jar;
    protected $cookie;

    public function __construct()
    {
        parent::__construct();
        $this->cookie_jar = dirname(__FILE__)."/cookie.txt";
    }


    /**
     * 从首页获取 cookie
     * @return mixed|string
     */
    public function getCookie()
    {

        //拉钩的cookie 不是在首页生成的 而是放到了搜索页面  二级页面下， 真的是防不胜防啊 ^V^ 大概说的是我吧。
        $url = 'https://m.lagou.com/search.html';
        if (file_exists($this->cookie_jar)){
            unlink($this->cookie_jar);
        }
        $curl = curl_init($url);
        curl_setopt_array($curl,[
            CURLOPT_RETURNTRANSFER =>true,
            CURLOPT_REFERER =>'https://m.lagou.com/search.html',
            CURLOPT_USERAGENT =>'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Mobile Safari/537.36',
            CURLOPT_COOKIEJAR => $this->cookie_jar,
            CURLOPT_COOKIEFILE => $this->cookie_jar,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
        ]);
        //3.发送请求
        $content = curl_exec($curl);
        $content = json_decode($content);
        $err = curl_error($curl);
        if (!empty($err)){
            return $err;
        }else{
            curl_close($curl);
            return $content;
        }
    }





    public function getLagouData($city = '全国',$page = 1)
    {
        $this->url  = 'https://m.lagou.com/search.json?city='.urlencode($city).'&positionName=PHP&pageNo='.$page.'&pageSize=15';

        //只在第一页的时候获取一下 cookie  翻页不再使用   不知道为什么第三页就用不了了，先每次都写入到文件吧
//        if ($page == 1){
            $this->getCookie();
//        }
       $data = $this->request($this->url,true);

        if (empty($data['content']['data']['page']['result'])){
            return [1,[]];
        }else{
            //返回 总页数  和数据  总页数用作分页数据
            return [ceil($data['content']['data']['page']['totalCount'] / $data['content']['data']['page']['pageSize']),$data['content']['data']['page']['result']];
        }
    }

    //请求方法
    public function request($url,$https=true,$method='get',$data=null){


        //1.初始化
        $ch = curl_init($url);
        //2.设置curl
        //返回数据不输出  在启用 CURLOPT_RETURNTRANSFER 的时候，返回原生的（Raw）输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        //CURLOPT_REFERER在HTTP请求头中"Referer: "的内容。
        curl_setopt($ch, CURLOPT_REFERER, 'https://m.lagou.com/search.html');

        //在HTTP请求中包含一个"User-Agent: "头的字符串。
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Mobile Safari/537.36');


        curl_setopt($ch,CURLOPT_COOKIEFILE, $this->cookie_jar);  //发送//存cookie的文件名，
//        curl_setopt( $ch,CURLOPT_COOKIEJAR,$this->cookie_jar);
        //满足https
        if($https === true){
            //绕过ssl验证
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        //满足post
        if($method === 'post'){
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //3.发送请求
        $content = curl_exec($ch);
        //4.关闭资源
        curl_close($ch);
        return json_decode($content,true);
    }



}